/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility.CustomTag;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Tran Nguyen Anh Tu
 */
public class CitySelectorTag extends SimpleTagSupport {

    private String city;

    public void setCity(String city) {
        this.city = city;
    }
    private ArrayList cityList;

    private void cityInit() {
        cityList = new ArrayList();
        cityList.add("Hồ Chí Minh");
        cityList.add("Đà Nẵng");
        cityList.add("Hà Nội");
        cityList.add("Hải Phòng");
        cityList.add("Cần Thơ");
        cityList.add("Phú Yên");
    }

    private void writeOutput() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();

        out.print("<select name=\"city\">");
        for (int i = 0; i < cityList.size(); i++) {
            if (city.equals(cityList.get(i))) {
                out.print("<option selected>" + cityList.get(i) + "</option>");
            } else {
                out.print("<option>" + cityList.get(i) + "</option>");
            }
        }
        out.print("</select>");
    }

    @Override
    public void doTag() throws JspException, IOException {
        this.cityInit();
        this.writeOutput();
    }
}
